package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.entity.PageBean;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工接口")
public class EmployeeController {

    // 员工添加，业务层
    @Autowired
    private EmployeeService employeeService;

    // jwt 令牌
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());

        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        log.info("token：{}", token);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("退出")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 添加员工
     */
    @PostMapping
    @ApiOperation("添加员工")
    public Result<String> save(@RequestBody EmployeeDTO employeeDTO) {

        log.info("添加员工数据，数据值为：{}", employeeDTO.toString());
        // 调用业务层，添加员工
        employeeService.save(employeeDTO);

        return Result.success();
    }


    /**
     * 员工分页查询
     */
    @GetMapping("/page")
    @ApiOperation("员工分页查询")
    Result page(String name,
                @RequestParam(defaultValue = "1") Integer page,
                @RequestParam(defaultValue = "10") Integer pageSize) {


        // 查看传递过来的数据
        log.info("传递数据为：name = {}，page = {}，pageSize = {}", name, page, pageSize);

        // 调用业务层
        PageBean pageBean = employeeService.page(name, page, pageSize);

        log.info("{}", pageBean);

        return Result.success(pageBean);
    }


    /**
     * 启用、禁用员工账号
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用、禁用员工账号")
    Result<String> EnableDisableEmployee(@PathVariable Integer status, Long id) {

        log.info("启用、禁用员工账号,传递数据为 status = {},id = {}", status, id);

        // 调用业务层--->启用、禁用员工账号
        employeeService.EnableDisableEmployee(status, id);

        return Result.success();
    }


    /**
     * 根据id查询员工
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询员工")
    Result idEmployee(@PathVariable Integer id) {

        log.info("根据id查询员工,传递数据为 ,id = {}", id);

        // 调用业务层--->根据id查询员工
        EmployeeDTO e = employeeService.idEmployee(id);


        return Result.success(e);
    }


    /**
     * 编辑员工信息
     */
    @PutMapping
    @ApiOperation("编辑员工信息")
    Result EditorialStaff(@RequestBody EmployeeDTO employeeDTO) {

        log.info("编辑员工信息,传递数据为 ,employeeDTO = {}", employeeDTO.toString());
        // 调用业务层--->编辑员工信息
        employeeService.EditorialStaff(employeeDTO);


        return Result.success();

    }


    /**
     * 员工修改密码
     */
    @PutMapping("/editPassword")
    @ApiOperation("员工修改密码")
    Result<String> editPassword(@RequestBody PasswordEditDTO passwordEditDTO) {

        passwordEditDTO.setEmpId(BaseContext.getCurrentId());
        log.info("员工修改密码，传递数据为：{}", passwordEditDTO.toString());

        // 调用业务层--->员工修改密码
        employeeService.editPassword(passwordEditDTO);

        return Result.success();
    }


}
